var tab1 = '普通密码';
var tab2 = '胁迫密码';
var swiper1,swiper;

var fingerKeyList = '';
var cardList = '';
var passList = '';

var slide = 1;
var refreah = 1;

var hisHtml = '';
var alarmHtml = '';

var beforeHtml = '';
var isDebug = 0;

Date.prototype.Format = function(fmt)   
{ //author: meizz   
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

function gettime(){
	var time = new Date().Format("yyyy-MM-dd hh:mm:ss");
	console.log('gettime:',time);
	return time;
}

function getHistoryParam(param,code){
	Hejia.getHistoryParam(param, function (arr) {
		console.log(arr,code);
		$('#elecDate').text(new Date().Format('MM-dd hh:mm'));
		if(code == 'openNotify'){
			if(arr.length == 0){
				if(alarmHtml == ''){
					$('.hisList').hide();
					$('.noinfo').show();
				}
				return;
			}
			let list = arr;
			let html = '';
			
			for(let i = 0;i<list.length;i++){
				let res = parse_852(list[i].params[0].value,{});
				let type = '';
				let css = '';
				if(res.state == 1){
					type = '普通指纹'
				} else if(res.state == 2){
					type = '胁迫指纹'
					css = 'alarm';
				} else if(res.state == 3){
					type = '普通密码'
				} else if(res.state == 4){
					type = '胁迫密码'
					css = 'alarm';
				} else if(res.state == 5){
					type = '临时密码'
				} else if(res.state == 6){
					type = '普通卡片'
				} else if(res.state == 7){
					type = '安全模式'
				}
				html = '<li time="'+list[i].timeStampMs+'" class="swiper-slide '+ css +'"><p><em></em>'+new Date(res.openTime).Format('hh:mm:ss')+'</p><p>'+ type + (res.state == 7 || res.state == 5 ? '' : ('0'+res.userid).substr(-2,2)) + ' 开锁</p></li>' + html;
			}
			hisHtml = html;
			html = alarmHtml + html;
			if(beforeHtml == html){
				return;
			} else {
				beforeHtml = html;
			}
			if(html == ''){
				$('.hisList').hide();
				$('.noinfo').show();
			} else {
				$('.hisList').show();
				$('.noinfo').hide();
				$('.hisList ul').empty();
				$('.hisList ul').append(html);
				let aDiv = $('.hisList ul li');
				let arr1 = [];
				for(let i=0;i<aDiv.length;i++)
				{
					arr1.push(aDiv[i]);
				}
				arr1.sort(function(a,b){return b.getAttribute('time') - a.getAttribute('time')});
				$('.hisList ul').empty();
				for(let i=0;i<arr1.length;i++){
					$('.hisList ul').append(arr1[i]);
				}
				hisList.update();
			}
		} else if(code == 'fingerKeyList'){
			if(arr.length == 0){
				$('#list3').empty();
				$('#list4').empty();
				$('#list3').append('<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>');
				$('#list4').append('<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>');
				return;
			}
			fingerKeyList = parse_852(arr[arr.length-1].params[0].value,{});
			console.log(fingerKeyList);
			let list = fingerKeyList.userlist;
			$('#finger b').text(list.length);
			let html = '';
			let html1 = '';
			for(let i = 0;i<list.length;i++){
				if(list[i].type == 1){
					html += '<li class="list"><span class="id">'+('0'+list[i].id).substr(-2,2)+'</span><span class="name"></span><span class="editDate">'+new Date(list[i].time).Format('yyyy-MM-dd')+'</span></li>'
				} else {
					html1 += '<li class="list"><span class="id">'+('0'+list[i].id).substr(-2,2)+'</span><span class="name"></span><span class="editDate">'+new Date(list[i].time).Format('yyyy-MM-dd')+'</span></li>'
				}
			}
			if(html == ''){
				html = '<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>'
			}
			$('#list3').empty();
			$('#list3').append(html);
			if(html1 == ''){
				html1 = '<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>'
			}
			$('#list4').empty();
			$('#list4').append(html1);
		} else if(code == 'cardList'){
			if(arr.length == 0){
				$('#list5').empty();
				$('#list5').append('<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>');
				return;
			}
			cardList = parse_852(arr[arr.length-1].params[0].value,{});
			let list = cardList.userlist;
			$('#card b').text(list.length);
			let html = '';
			for(let i = 0;i<list.length;i++){
				html += '<li class="list"><span class="id">'+('0'+list[i].id).substr(-2,2)+'</span><span class="name"></span><span class="editDate">'+new Date(list[i].time).Format('yyyy-MM-dd')+'</span></li>'
			}
			if(html == ''){
				html = '<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>'
			}
			$('#list5').empty();
			$('#list5').append(html);
		} else if(code == 'passList'){
			if(arr.length == 0){
				$('#list1').empty();
				$('#list2').empty();
				$('#list1').append('<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>');
				$('#list2').append('<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>');
				return;
			}
			passList = parse_852(arr[arr.length-1].params[0].value,{});
			let list = passList.userlist;
			$('#pwd b').text(list.length);
			let html = '';
			let html1 = '';
			for(let i = 0;i<list.length;i++){
				if(list[i].type == 1){
					html += '<li class="list"><span class="id">'+('0'+list[i].id).substr(-2,2)+'</span><span class="name"></span><span class="editDate">'+new Date(list[i].time).Format('yyyy-MM-dd')+'</span></li>'
				} else {
					html1 += '<li class="list"><span class="id">'+('0'+list[i].id).substr(-2,2)+'</span><span class="name"></span><span class="editDate">'+new Date(list[i].time).Format('yyyy-MM-dd')+'</span></li>'
				}
			}
			if(html == ''){
				html = '<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>'
			}
			$('#list1').empty();
			$('#list1').append(html);
			if(html1 == ''){
				html1 = '<p class="noinfo"><img src="img/nohis.png" /><span>请在锁端进行添加操作</span></p>'
			}
			$('#list2').empty();
			$('#list2').append(html1);
		} else if(code == 'batteryInfo'){
			if(arr.length == 0){
				return;
			}
			let batteryInfo = arr[arr.length-1].params[0].value;
			$('#elec').text(batteryInfo);
			$('.barwidth').css('width',batteryInfo+'%');
			if(batteryInfo<=25){
				$('.electric').css('color','rgba(250, 75, 75, 1)');
				$('.lowElec').show();
				$('.electric .bar img').attr('src','img/lowelec.png');
				$('.barwidth').css('background','rgba(250, 75, 75, 1)');
			} else {
				$('.electric').css('color','rgba(255, 255, 255, 1)');
				$('.lowElec').hide();
				$('.electric .bar img').attr('src','img/elec.png');
				$('.barwidth').css('background','rgba(255, 255, 255, 1)');
			}
		} else if(code == 'unlockException'){
			if(arr.length == 0){
				if(hisHtml == ''){
					$('.hisList').hide();
					$('.noinfo').show();
				}
				return;
			}
			let list = arr;
			let html = '';
			for(let i = 0;i<list.length;i++){
				let type = list[i].params[0].value;
				let alarm = '';
				if(type == 1){
					alarm = '低电量'
				} else if(type == 2){
					alarm = '防撬报警'
				} else if(type == 3){
					alarm = '防试开报警'
				} else if(type == 4){
					alarm = '劫持报警'
				} else if(type == 5){
					alarm = '侵入报警'
				} else if(type == 6){
					alarm = '非法侵入'
				} else if(type == 7){
					alarm = '门未关好'
				} else if(type == 0){
					continue;
				}
				html = '<li time="'+list[i].timeStampMs+'" class="swiper-slide alarm"><p><em></em>'+new Date(list[i].timeStampMs).Format('hh:mm:ss')+'</p><p>'+ alarm + '</p></li>' + html;
			}
			alarmHtml = html;
			html = hisHtml + html;
			if(beforeHtml == html){
				return;
			} else {
				beforeHtml = html;
			}
			if(html == ''){
				$('.hisList').hide();
				$('.noinfo').show();
			} else {
				$('.hisList').show();
				$('.noinfo').hide();
				$('.hisList ul').empty();
				$('.hisList ul').append(html);
				let aDiv = $('.hisList ul li');
				let arr1 = [];
				for(let i=0;i<aDiv.length;i++)
				{
					arr1.push(aDiv[i]);
				}
				arr1.sort(function(a,b){return b.getAttribute('time') - a.getAttribute('time')});
				$('.hisList ul').empty();
				for(let i=0;i<arr1.length;i++){
					$('.hisList ul').append(arr1[i]);
				}
				hisList.update();
			}			
		}
	}, function (msg, obj){
		console.log(msg);
		console.log(obj.resultCode + obj.resultCodeMessage);
	});
}

var year = '';
var month = '';
var day = '';
var date = new Date();
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
for(let i=2021;i<=new Date().getFullYear();i++){
	year += '<li class="swiper-slide">'+i+'</li>';
}
for(let i=1;i<=new Date().getMonth()+1;i++){
	month += '<li class="swiper-slide">'+i+'</li>';
}
for(let i=1;i<=new Date().getDate();i++){
	day += '<li class="swiper-slide">'+i+'</li>';
}

var swiperYear, swiperMonth, swiperDay, hisList;
var deviceId = "";
var API_KEY = "";
// var tmpPwds = "";
let createPwdReqRes = null;
var deadlineRes = "";
// 生成临时密码
function createPwdReq() {
	var result = "";
	//1. js获取当前时间
	var date = new Date();
	//2. 获取当前分钟
	var min = date.getMinutes();
	//3. 设置当前时间+5分钟：把当前分钟数+5后的值重新设置为date对象的分钟数
	date.setMinutes(min + parseInt(deadlineRes));
	console.log('date:', date);
	console.log('deadlineRes', deadlineRes);
	console.log(typeof deadlineRes);
	var tmpPwdEndTime = date.Format("yyyy-MM-dd hh:mm:ss");
	var tmpPwdStartTime = gettime()
	console.log('tmpPwdEndTime:', tmpPwdEndTime);
	console.log('tmpPwdStartTime:', tmpPwdStartTime);
	var apiKey;
	var tempDeviceId;
	if(isDebug == 0){
	    tempDeviceId = deviceId;
		apiKey = API_KEY;
	}
	else{
		tempDeviceId = "CMCC-589585-000EA34ABCF1";
		apiKey = "5b0b8152dab74cb6b15c1467f13d93b8";
	}
	var data = {
		"deviceId": tempDeviceId,
		// "deviceId": "CMCC-590041-000EA36EE3AF",
		"tmpPwdUserId": "2222222222222",
		"tmpPwdStartTime": tmpPwdStartTime,
		"tmpPwdEndTime": tmpPwdEndTime,
		"tmpPwdNum": 5,
		"maxValidPwdNum": 1,
		"userBindVerify": false
	}
	$.ajax({
		type: 'POST',
		url: 'https://andlink.komect.com/espapi/cloud/json/devices/passwd',
		headers: {
			"Content-Type": "application/json",
			"API_KEY": apiKey,
			// "API_KEY": "5b0b8152dab74cb6b15c1467f13d93b8"

		},
		// query:{
		// 	onlyAvailable :true
		// },
		data: JSON.stringify(data),
		success: function (res) {
			result = res;
			console.log('createPwdReq:', res);
			// console.log('result:',result);
			createPwdReqRes = result;
			tempPwdReq(createPwdReqRes)
			// if(res.retCode == 200){
			// 	sysTime = res.data.systemTime;
			// }
		},
		error: function (res) {
			console.log(res);
		}

	})

}
// 获取临时密码
var shownum = 1;
function tempPwdReq(result) {
	console.log('deviceId:', deviceId);
	
	var pwdUrl;
	var apiKey;
	if(isDebug == 0){
		pwdUrl = "https://andlink.komect.com/espapi/cloud/json/devices/passwd/" + deviceId;
		apiKey = API_KEY;
	}
	else{
		pwdUrl = "https://andlink.komect.com/espapi/cloud/json/devices/passwd/CMCC-589585-000EA34ABCF1";
		apiKey = "5b0b8152dab74cb6b15c1467f13d93b8";
	}
	

	console.log('pwdUrl:', pwdUrl);
	$.ajax({
		type: 'GET',
		url: pwdUrl,
		headers: {
			"Content-Type": "application/json;charset=utf-8",
			"API_KEY": apiKey,
			
		},
		query: {
			onlyAvailable: true
		},
		success: function (res) {
			console.log('tempPwdReq:', res);
			result = res.tmpPwds.length;
			console.log('result:',res.tmpPwds.length);
			if(0 == res.tmpPwds.length ){
				$('#lsmmadd').show();
				// console.log('显示:',lsmmadd);
			}else{
				$('#lsmmpage').show();
			}
			if (res.resultCode == 0) {
				// console.log('tmpPwds:',res.tmpPwds[0]);
				$("#pwdList").empty()//清空当前节点里面的所有数据
				
			 	var inUsePassNum =0;
				
				for (let i = 0; i < res.tmpPwds.length; i++) {
					tmpPwds = res.tmpPwds[i]
					tmpPwdId = res.tmpPwds[i].tmpPwdId
					console.log('tmpPwdId:', tmpPwdId);
					var pwd = tmpPwds.tmpPwdKey + '';
					var loseTime = tmpPwds.tmpPwdEndTime
					$('.temporaryList').remove()
					// $('#pwdInput').val(pwd);
					$("#pwdList").append('<div data-pid="' + tmpPwdId + '" class="temporary"><div class="temporaryPwd"><p class="tpwd"><span>' + pwd[0] + '</span><span>' + pwd[1] + '</span><span>' + pwd[2] + '</span><span>' + pwd[3] + '</span><span>' + pwd[4] + '</span><span>' + pwd[5] + '</span><p><p id="copy" class="copy"><img src="img/copy.png" /><input class="pwdInput" type="text" value="临时密码：'+pwd+';失效时间：密码将于'+loseTime+'失效;操作步骤：使用前，请先在门锁上输入*0#唤醒门锁，再根据门锁语音提示输入临时密码"/></p><p style="padding-top: 40px;" class="validity val">' + '密码将于' + loseTime + '失效' + '</p><p class="validity validityno" style="width: 4.52rem;">使用前，请先在门锁上输入*0#唤醒门锁，再根据门锁语音提示输入临时密码</p></div></div>')
					var tmpPwdEndTime = new Date(tmpPwds.tmpPwdEndTime).getTime();
					var currentRunTime = new Date().getTime();
					// console.log('currentRunTime:',currentRunTime);
					// console.log('tmpPwdEndTime:',tmpPwdEndTime);

					if (tmpPwdEndTime < currentRunTime) {

						// 	// res.tmpPwds.slice(-1)
						$('.temporary').css({ 'background': '#999999', 'height': '3.06rem' });
						$('.temporaryPwd span ').css('color', '#999999');
						$('.copy img').attr('src', 'img/rubbish.png');
						$('.copy').attr('class', 'rubbish');
						$("p.val").replaceWith('<p style="padding-top: 40px;" class="validity val">密码已失效</p>')
						$('.validityno').remove();
						// $('.copyadd').show();
						// res.tmpPwds.length = 0
						// console.log(1);
						// createPwdReq();
						// deletePwdReq();
                        

					}
					else
					{
						inUsePassNum++;

					}


				}

				console.log("inUsePassNum",inUsePassNum);
				
				if( inUsePassNum >= shownum){
					console.log('result1:',res.tmpPwds.length);
						$('.copyadd').hide();
					}else{
					console.log('result2:',res.tmpPwds.length);
						$('.copyadd').show();
					}

				$('.copy').click(function(e){
					console.log("点击了");
					console.log(e.target.value)
					console.log(e)
					if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
						var Url2=e.target;
						Url2.select();
						Url2.setSelectionRange(0,Url2.value.length);
						document.execCommand("Copy");
						Url2.blur();
						toast('复制成功');
					} else {
						var Url2=e.target;
						Url2.select();
						Url2.blur();
						document.execCommand("Copy");
						toast('复制成功');
					}
				});
				$('.rubbish').click(function () {

					// console.log('tmpPwdId:',tmpPwdId);
					var pid = $(this).parents(".temporary").attr("data-pid")
					console.log("就是这个数据", pid);
					deletePwdReq(pid)
					$(this).parents(".temporary").remove()
                    var addLength = document.querySelectorAll(".temporary")
					console.log(addLength.length);
					
					if(addLength.length <= shownum-1){
						console.log('小于');
						$('.copyadd').show();
					}else{
						console.log('大于');
					}
                    if(0==addLength.length){
						$('.copyadd').hide();
						$('#lsmmpage').hide();
						$('#lsmmadd').show();
						console.log('空页面');
					}
				});

				// console.log('tmpPwdId:',tmpPwdId);

				//jie mian zhanshi
				//  console.log('result', result)
				//  console.log('createPwdReqRes:',createPwdReqRes);
				// var pwd = result.tmpPwd.tmpPwdKey + '';
				// var loseTime = result.tmpPwd.tmpPwdEndTime
				// console.log('loseTime:',loseTime);
				// $('#pwdInput').val(pwd);
				// pwd = pwd.split('');
				// $(".tpwd span").remove();
				// for(i=0;i<pwd.length;i++) {
				// 	$(".tpwd").append('<span>'+pwd[i]+'</span>');
				// }
				// $("p.val").replaceWith('<p style="padding-top: 40px;" class="validity val">'+'密码将于'+loseTime+'失效'+'</p>')
			}
		},
		error: function (res) {
			console.log(res);
		}
	})
}
// 删除临时密码
function deletePwdReq(pid) {
	console.log(pid);
	var delUrl;
	var apiKey;
	var tempDeviceId;
	var tempPId;
	// var delUrl = 'https://andlink.komect.com/espapi/cloud/json/devices/passwd?tmpPwdId=' + pid + '&deviceId=CMCC-589585-000EA34ABCF1';
	if(isDebug == 0){
	    tempDeviceId = deviceId;
		apiKey = API_KEY;
		tempPId = pid;
		delUrl =  "https://andlink.komect.com/espapi/cloud/json/devices/passwd" + "?tmpPwdId=" + pid + "&deviceId=" + deviceId;;
	}
	else{
		tempDeviceId = "CMCC-589585-000EA34ABCF1";
		apiKey = "5b0b8152dab74cb6b15c1467f13d93b8";
		tempPId = pid;
		delUrl = 'https://andlink.komect.com/espapi/cloud/json/devices/passwd?tmpPwdId=' + pid + '&deviceId=CMCC-589585-000EA34ABCF1';
	}
	$.ajax({
		type: 'DELETE',
		url: delUrl,
		headers: {
			"Content-Type": "application/json;charset=utf-8",
			"API_KEY": apiKey,
			// "API_KEY": "5b0b8152dab74cb6b15c1467f13d93b8",
		},
		query: {
			"tmpPwdId": tempPId,
			// "tmpPwdId": pid,
			"deviceId": tempDeviceId,
			// "deviceId": "CMCC-589585-000EA34ABCF1",
		},
		success: function (res) {
			console.log('deletePwdReq:', res);
			// location.reload();
			// tempPwdReq();
		},
		error: function (res) {
			console.log(res);
		}
	})
}
$(document).ready(function (e) {
/*	document.addEventListener("touchmove", function (e) {
		e.preventDefault()
	}, {passive: false});*/


	$('.selectDate').text((date.getMonth()+1)+'/'+date.getDate());
	$('.week').text(weeks[date.getDay()]);

	var vconsole = new VConsole();

	Hejia.ready(function(){
		let date1 = new Date();
		let time = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate());
		let time1 = new Date(2021,1,1);
/*		Hejia.getUserRuleList(function (obj) {
			console.log('getUserRuleList',obj);
		}, function (msg, obj){
			console.log('getUserRuleList',msg,obj);
		});*/
		Hejia.getApiKey(function (apiKey) {
			API_KEY = (apiKey);
		   console.log('apiKey:',apiKey);
		   console.log('API_KEY:',API_KEY);
		}, function (){
		   //do something
		});
		// Hejia.getDeviceId(function (version) {
		// 	// IMEI - string => deviceId
		// 	console.log('version:',version);
		// 	console.log('IMEI:',IMEI);
		// }, function (){
		//    //do something
		// });
		Hejia.getDeviceInfo(function (obj) {
			console.log(obj.device.id);
			deviceId = (obj.device.id);
			console.log('deviceId:',deviceId);
			$('#lineState').text(obj.device.connected == true?'在线':'离线');
		}, function (msg, obj){
			console.log(msg,obj);
		});
		getHistoryParam({startDate: time.getTime(),endDate: time.getTime()+86400000,paramName: ['openNotify']},'openNotify');
		getHistoryParam({startDate: time.getTime(),endDate: time.getTime()+86400000,paramName: ['unlockException']},'unlockException');
		setTimeout(function(){
			getHistoryParam({startDate: time1.getTime(),paramName: ['batteryInfo']},'batteryInfo');
		},50);
		setTimeout(function(){
			getHistoryParam({startDate: time1.getTime(),paramName: ['fingerKeyList']},'fingerKeyList');
		},100);
		setTimeout(function(){
			getHistoryParam({startDate: time1.getTime(),paramName: ['cardList']},'cardList');
		},150);
		setTimeout(function(){
			getHistoryParam({startDate: time1.getTime(),paramName: ['passList']},'passList');
		},200);
		
		
		setInterval(function(){
			if(refreah == 1){
				getHistoryParam({startDate: time.getTime(),paramName: ['openNotify']},'openNotify');
				getHistoryParam({startDate: time.getTime(),paramName: ['unlockException']},'unlockException');
			}
			getHistoryParam({startDate: time1.getTime(),paramName: ['batteryInfo']},'batteryInfo');
			getHistoryParam({startDate: time1.getTime(),paramName: ['fingerKeyList']},'fingerKeyList');
			getHistoryParam({startDate: time1.getTime(),paramName: ['cardList']},'cardList');
			getHistoryParam({startDate: time1.getTime(),paramName: ['passList']},'passList');
		},5000);
		
		
	});
	
	$('.year ul').append(year);
	$('.month ul').append(month);
	$('.day ul').append(day);


	
	swiper = new Swiper("#swiper",{
//		freeMode: true,
		observer: true,
		observeParents: true,
		touchRatio : 0,//触摸变慢
		pagination: {
			el: '.setTab1',
			type: 'custom',
			clickable: true,
			renderCustom: function(swiper,current, total){
				var paginationHtml = "";
				let name = '';
				for(var i= 0; i< total; i++) {
					name = i == 0 ? '普通密码' : '胁迫密码';
					var id = i == 0 ? 'tab':'';
					if(i === (current -1)){
						paginationHtml += '<span id="'+id+'" class="swiper-pagination-bullet swiper-pagination-bullet-active"><b>'+name+'</b><em></em></span>';
					} else {
						paginationHtml += '<span id="'+id+'" class="swiper-pagination-bullet"><b>'+name+'</b></span>';
					}
				}
				return paginationHtml;
			}
		},
	});
	swiper1 = new Swiper("#swiper1",{
//		freeMode: true,
		observer: true,
		observeParents: true,
		touchRatio : 0,//触摸变慢
		pagination: {
			el: '.setTab2',
			type: 'custom',
			clickable: true,
			renderCustom: function(swiper,current, total){
				var paginationHtml = "";
				let name = '';
				for(var i= 0; i< total; i++) {
					name = i == 0 ? '普通指纹' : '胁迫指纹';
					var id = i == 0 ? 'tab':'';
					if(i === (current -1)){
						paginationHtml += '<span id="'+id+'" class="swiper-pagination-bullet swiper-pagination-bullet-active"><b>'+name+'</b><em></em></span>';
					} else {
						paginationHtml += '<span id="'+id+'" class="swiper-pagination-bullet"><b>'+name+'</b></span>';
					}
				}
				return paginationHtml;
			}
		},
	});
	
	swiperYear = new Swiper('.year',{
		direction: "vertical",
		slidesPerView: "5",
		observer: true,
		observeParents: true,
		centeredSlides: true,
		on: {
			transitionEnd: function(s){
				try {
					month = '';
					if(swiperYear.activeIndex == new Date().getFullYear()-2021){
						for(let i=1;i<=new Date().getMonth()+1;i++){
							month += '<li class="swiper-slide">'+i+'</li>';
						}
					} else {
						for(let i=1;i<=12;i++){
							month += '<li class="swiper-slide">'+i+'</li>';
						}
					}
					$('.month ul').empty();
					$('.month ul').append(month);
				} catch(e){
				}
			}
		}
	});
	swiperMonth = new Swiper('.month',{
		direction: "vertical",
		slidesPerView: "5",
		observer: true,
		observeParents: true,
		centeredSlides: true,
		on: {
			transitionEnd: function(s){
				try {
					day = '';
					if(swiperYear.activeIndex == new Date().getFullYear()-2021 && swiperMonth.activeIndex == new Date().getMonth()){
						for(let i=1;i<=new Date().getDate();i++){
							day += '<li class="swiper-slide">'+i+'</li>';
						}
					} else {
						if(swiperMonth.activeIndex == 1){
							if((swiperYear.activeIndex+2021)%4 == 0) {
								for(let i=1;i<=29;i++){
									day += '<li class="swiper-slide">'+i+'</li>';
								}
							} else {
								for(let i=1;i<=28;i++){
									day += '<li class="swiper-slide">'+i+'</li>';
								}
							}
						} else if(swiperMonth.activeIndex == 0 || swiperMonth.activeIndex == 2 || swiperMonth.activeIndex == 4 || swiperMonth.activeIndex == 6 || swiperMonth.activeIndex == 7 || swiperMonth.activeIndex == 9 || swiperMonth.activeIndex == 11){
							for(let i=1;i<=31;i++){
								day += '<li class="swiper-slide">'+i+'</li>';
							}
						} else if(swiperMonth.activeIndex == 3 || swiperMonth.activeIndex == 5 || swiperMonth.activeIndex == 8 || swiperMonth.activeIndex == 10){
							for(let i=1;i<=30;i++){
								day += '<li class="swiper-slide">'+i+'</li>';
							}
						}
					}
					$('.day ul').empty();
					$('.day ul').append(day);
				} catch(e){
				}
			}
		}
	});
	swiperDay = new Swiper('.day',{
		direction: "vertical",
		slidesPerView: "5",
		observer: true,
		observeParents: true,
		centeredSlides: true
	});
	hisList = new Swiper('.hisList',{
		direction: "vertical",
		freeMode : true,
		freeModeMomentumBounce : false,
		freeModeMomentumVelocityRatio: 0.3,
		slidesPerView: "auto",
		observer: true,
		observeParents: true,
	});
	
	
	$('.selectBtn').click(function(){
		if(slide == 1){
			slide = 0;
			swiperYear.slideTo(date.getFullYear()-2021,500,false);
			swiperMonth.slideTo(date.getMonth(),500,false);
			swiperDay.slideTo(date.getDate()-1,500,false);
		}
		$('.datePicker,#mb').show();
	});
	$('.datePicker .title span:nth-child(1),.datePicker .title span:nth-child(3)').click(function(){
		$('.datePicker,#mb').hide();
	});
	$('.datePicker .title span:nth-child(3)').click(function(){
		let time = new Date(swiperYear.activeIndex+2021,swiperMonth.activeIndex,swiperDay.activeIndex+1);
		let startDate = time.getTime();
		let endDate = startDate+86400000;
		if(startDate < new Date().getTime() && endDate > new Date().getTime()){
			refreah = 1;
		} else {
			refreah = 0;
		}
		$('.selectDate').text((time.getMonth()+1)+'/'+time.getDate());
		$('.week').text(weeks[time.getDay()]);
		hisHtml = '';
		alarmHtml = '';
		getHistoryParam({startDate: startDate, endDate: endDate, paramName: ['openNotify']},'openNotify');
		getHistoryParam({startDate: startDate, endDate: endDate, paramName: ['unlockException']},'unlockException');
	});
	
	$('#ysgl').click(function(){
		$('.manage').show();
	});
	$('.manage .back').click(function(){
		$('.manage').hide();
	});
	$('#lsmm').click(function(){
		// tempPwdReq();
		// if (createPwdReqRes == null)  {
		// 	createPwdReq();
		// } else {
		// 	tempPwdReq(createPwdReqRes)
		// }
		// $('#lsmmpage').show();


	   // 1.获取临时密码
	   tempPwdReq(createPwdReqRes)
	   console.log('createPwdReqRes:',createPwdReqRes);
	});
	$('.copyadd').click(function(){
		console.log('copyadd');
		$('#lsmmtime').show();
	});
	$('#lsmmpage .back').click(function(){
		$('#lsmmpage').hide();
	});
	$('#lsmmadd .back').click(function () {
		$('#lsmmadd').hide();
	});
	$('#lsmmtime .back').click(function () {
		$('#lsmmtime').hide();
	});
	$('#lsmmadd .add_content').click(function () {
		$('#lsmmtime').show();
	});
	$('#lsmmtime .deadlinebtn').click(function () {
		var str = $('input:radio:checked').val()
		if (str == 5) {
			deadlineRes = str
			// alert(deadlineRes)
		} else if (str == 10) {
			deadlineRes = str
			// alert(deadlineRes)
		} else if (str == 20) {
			deadlineRes = str
			// alert(deadlineRes)
		}
		createPwdReq();
		// if (createPwdReqRes == null) {
		// 	createPwdReq();
		// 	console.log("创建");
		// } else {
		// 	console.log("查询");
		// 	tempPwdReq(createPwdReqRes)
		// }
		$('#lsmmadd').hide();
		$('#lsmmtime').hide();
		$('#lsmmpage').show();
	});
	
	$('.card').click(function(){
		$('#public').show();
		var id = $(this).attr('id');
		pageid = id;
//		$("#list1,#list2,#list3").empty();
		if(id == 'pwd'){
			$('#public .title').text('密码管理');
			$('.setTab1,#swiper').show();
			$('.cardList,#swiper1,.setTab2').hide();
		} else if(id == 'card'){
			$('#public .title').text('门卡管理');
			$('.cardList').show();
			$('.setTab1,#swiper,#swiper1,.setTab2').hide();
		} else {
			$('#public .title').text('指纹管理');
			$('.setTab2,#swiper1').show();
			$('.cardList,#swiper,.setTab1').hide();
		}
	});

	$('#public .back').click(function(){
		$('#public').hide();
	});
	$('#edit .back').click(function(){
		$('#edit').hide();
	});
	
	$('.lowElec img').click(function(){
		$("#mb,.wrapper").show();
	});
	$('.wrapper .errorClose').click(function(){
		$("#mb,.wrapper").hide();
	});
	
	$(document).on('click','.list',function(){
		let id = $(this).children('.id').text();
		let time = $(this).children('.editDate').text();
		$('#edit').show();
		$('#id').text(id);
		$('#time').text(time);
	});
	
	// $('#copy').click(function(){
	// 	if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
	// 		var Url2=document.getElementById("pwdInput");
	// 		Url2.select();
	// 		Url2.setSelectionRange(0,Url2.value.length);
	// 		document.execCommand("Copy");
	// 		Url2.blur();
	// 		toast('复制成功');
	// 	} else {
	// 		var Url2=document.getElementById("pwdInput");
	// 		Url2.select();
	// 		Url2.blur();
	// 		document.execCommand("Copy");
	// 		toast('复制成功');
	// 	}
	// });
	
});


function toast(txt) {
	$(".toasttxt").text(txt);
	$(".toast").show();
	$(".toast").fadeOut(2000);
}

function encryptByDES(message, key){
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var message = CryptoJS.enc.Utf8.parse(message);
	console.log(keyHex);
    var encrypted = CryptoJS.TripleDES.encrypt(message, keyHex, {
		iv: keyHex,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.NoPadding
    });
    return encrypted.ciphertext.toString();
}





function toBinary (num) {
	let arr = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001'];
	let str = '';
	num = num.toString();
	for(let i = 0; i < num.length; i++){
		str = arr[i] + str;
	}

	return str;
}

